<template>
  <div class="a">
    <section>
      <div class="box">
        <!-- 登录框 -->
        <div class="container">
          <div class="form">
            <h2>登录</h2>
            <el-form ref="loginForm" :model="loginForm" :rules="loginRules">
              <el-form-item class="inputBox" prop="username">
                <el-input
                  type="text"
                  placeholder="请输入账号"
                  v-model="loginForm.username"
                />
              </el-form-item>
              <el-form-item class="inputBox" prop="password">
                <el-input
                  type="password"
                  placeholder="请输入密码"
                  v-model="loginForm.password"
                />
              </el-form-item>
              <el-form-item>
                <el-button
                  :loading="loading"
                  class="login_btn"
                  @click="handleLogin('loginForm')"
                >
                  <span v-if="!loading">登 录</span>
                  <span v-else>登 录 中...</span>
                </el-button>
              </el-form-item>
              <!-- <p class="forget"><a href="#"> 忘记密码 </a></p> -->
              <p class="forget"><a @click="register"> 注册账号 </a></p>
            </el-form>
          </div>
        </div>
      </div>
    </section>
  </div>
</template>

<script>
import api from "@api/index";
import {
  setToken,
  setUsername,
  setId,
  setEmail,
  setType,
  setisCommend,
  getisCommend,
} from "@/utils/auth";
// import axios from "axios";
export default {
  data() {
    return {
      loginForm: {
        username: "",
        password: "",
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "账号不能为空" },
        ],
        password: [
          { required: true, trigger: "blur", message: "密码不能为空" },
        ],
      },
      loading: false,
    };
  },
  methods: {
    handleLogin(loginForm) {
      // this.$router.push({ path: "/index" });

      this.$refs[loginForm].validate((valid) => {
        if (valid) {
          var params = this.loginForm;
          this.loading = true;
          api.login(params).then(
            (res) => {
              console.log(res);
              if (res.code === 200) {
                setToken(res.token);
                setUsername(res.data.username);
                setId(res.data.id);
                setEmail(res.data.email);
                setType(res.data.TypeList);
                setisCommend(res.data.isCommend);
                if (res.data.TypeList || res.data.isCommend) {
                  this.$router.push({ path: "/index" });
                } else {
                  this.$router.push({ path: "/interest" });
                }
              } else {
                this.$notify({
                  title: res.msg,
                  type: "error",
                  duration: 6000,
                });
              }
              this.loading = false;
            },
            (err) => {
              console.log(err);
            }
          );
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    register() {
      this.$router.push({ path: "/Register" });
    },
  },
};
</script>

<style scoped>
/* 使用flex布局，让内容垂直和水平居中 */

section {
  min-height: 100vh;
  background-image: url(../assets/images/bg3.jpg);
  background-size: cover;
  background-position: center center;
  display: flex;
  justify-content: center;
  align-items: center;
}

/* 登录框样式 */

.container {
  position: relative;
  width: 400px;
  min-height: 400px;
  background: rgba(255, 255, 255, 0.1);
  display: flex;
  justify-content: center;
  align-items: center;
  backdrop-filter: blur(5px);
  box-shadow: 0 25px 45px rgba(234, 231, 231, 0.1);
  border: 0;
}

.form {
  position: relative;
  width: 100%;
  height: 100%;
  padding: 50px;
}

/* 登录标题样式 */

.form h2 {
  position: relative;
  color: #fff;
  font-size: 24px;
  font-weight: 600;
  letter-spacing: 5px;
  margin-bottom: 30px;
  cursor: pointer;
  text-align: center;
}

.form .inputBox {
  width: 100%;
  margin-top: 20px;
}

/* 输入框样式 */

/deep/.el-input--small .el-input__inner {
  height: 38px;
  line-height: 38px;
  background-color: rgba(255, 255, 255, 0.5);
  border-radius: 20px;
  border: 1px solid #dcdfe6;
  box-sizing: border-box;
  color: #fff;
  display: inline-block;
  outline: 0;
  padding: 0 15px;
  transition: border-color 0.2s cubic-bezier(0.645, 0.045, 0.355, 1);
  width: 100%;
  font-size: 15px;
}
/deep/.el-input--small .el-input__inner::placeholder {
  color: #fff;
}

/* 登录按钮样式 */

.login_btn {
  background: #fff;
  color: #666;
  /* padding: 6px; */
  width: 70%;
  height: 40px;
  border-radius: 30px;
  font-size: 20px;
  font-family: "KaiTi";
  font-weight: 600;
  margin: 6% 15%;
}
.login_btn:hover {
  background-color: rgba(255, 255, 255, 0.5);
  animation: login_mation 0.5s;
  color: #666;
}

.forget {
  margin-top: 6px;
  color: #fff;
  letter-spacing: 1px;
  text-align: center;
}

.forget a {
  color: #fff;
  font-weight: 600;
  text-decoration: none;
}
</style>